﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using S.Breed.Domain;
using S.Breed.Domain.Dto;
using S.Breed.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace S.Breed.Infrastructure.Impl
{
    public class MaternalMatingRepository : BaseRepository<MaternalMatingModel>, IMaternalMatingRepository
    {
        private readonly MyDbContext db;

        public MaternalMatingRepository(MyDbContext db) : base(db)
        {
            this.db = db;
        }
        /// <summary>
        /// 配种新增
        /// </summary>
        public int AddMaternalMating(List<MaternalMatingModel> m)
        {
            db.MaternalMatingModel.AddRange(m);
            int rowsAffected = db.SaveChanges();

            return  rowsAffected;
        }
        /// <summary>
        /// 通过种母查种公
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public MaternalMatingModel ShowRamEar(string? ear)
        {
            var model = db.MaternalMatingModel
          .Where(t => t.EarTag == ear)
          .OrderByDescending(t => t.BreedingDate)
          .FirstOrDefault();
            return model;
        }
        /// <summary>
        /// 配种显示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<MaternalMatingModel> ShowShowMaternalMating(string? mdate, string? ear, int head)
        {
           var query = db.MaternalMatingModel.AsQueryable();
            if (!string.IsNullOrEmpty(mdate))
            {
                DateTime searchDate = DateTime.Parse(mdate);
                query = query.Where(t => t.BreedingDate.HasValue && t.BreedingDate.Value.Date == searchDate.Date);

            }
            if (!string.IsNullOrEmpty(ear))
            {
                query = query.Where(t => t.EarTag.Contains(ear));

            }
            if (head!=0)
            {
                query = query.Where(t => t.UserId == head);

            }
            var list = query.ToList();
            return list;
        }
    }
}
